package wb.is.besti.edu.cd.emergency_responsesys.utils;

import android.content.Context;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class GetLocation {

    public static List<MyLocations> getJSOnList(Context context) throws Exception{
        final RequestQueue queue = Volley.newRequestQueue(context);
        final List<MyLocations> locationsList = new ArrayList<>();
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest("http://localhost:8080/myServlet",new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                try {
                    for (int i=0;i<response.length();i++)
                    {
                        JSONObject jsonObject = response.getJSONObject(i);
                        float longitude = (float) jsonObject.getDouble("longitude");
                        float latitude = (float) jsonObject.getDouble("latitude");
                        locationsList.add(new MyLocations(longitude,latitude));
                    }
                }catch (JSONException e){
                    e.printStackTrace();
                }
                Log.e("TAG",response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG",error.getMessage(),error);
            }
        });
        queue.add(jsonArrayRequest);
        return locationsList;
    }
    public static List<MyLocations> getLocationsList() throws Exception{
        String path = "http://localhost:8080/myServlet";
        HttpURLConnection connection = (HttpURLConnection)new URL(path).openConnection();
        connection.setConnectTimeout(5000);
        connection.setRequestMethod("GET");
        InputStream json = connection.getInputStream();
        return parseJSON(json);


    }
    private static List<MyLocations> parseJSON(InputStream inputStream) throws Exception{
        List<MyLocations> locationsList = new ArrayList<>();
        byte[] data = read(inputStream);
        String json = new String(data);
        JSONArray jsonArray = new JSONArray(json);
        for (int i =0;i<jsonArray.length();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            float longitude = Float.valueOf(jsonObject.getString("longitude"));
            float latitude = Float.valueOf(jsonObject.getString("latitude"));
            locationsList.add(new MyLocations(longitude,latitude));
        }
        return locationsList;
    }

    public static byte[] read(InputStream inputStream) throws IOException {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int num = inputStream.read(buffer);
            while (num != -1) {
                baos.write(buffer, 0, num);
                num = inputStream.read(buffer);
            }
            baos.flush();
            return baos.toByteArray();
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

}
